package com.uubx.controller;

import com.uubx.pojo.User;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.UserService;
import com.uubx.vo.UserVo;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:31
 * @Version 1.0
 */
@Controller
@ResponseBody
@RequestMapping("/user")
@Api(description = "账户相关的接口文档")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询账户信息
     * @param page
     * @param limit
     * @return
     */

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询所有的账户",notes = "分页查询所有的账户",httpMethod = "GET",
    authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页数",required=true,paramType = "query"),
                        @ApiImplicitParam(name = "limit",value = "分页大小",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
                   @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllUserList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "limit",defaultValue = "5")Integer limit){

        if (page==null||page.equals("")){
            return ResultUtils.buildFail(MessageCode.PAGE_IS_EMPTY);
        }
        if (limit==null||limit.equals("")){
            return ResultUtils.buildFail(MessageCode.LIMIT_IS_EMPTY);
        }

        UserVo userVo=new UserVo();
        userVo.setStartIndex((page-1)*limit);
        userVo.setLimit(limit);
        List<User> users = userService.queryAllUser(userVo);

        Long count = userService.queryAllUserCount(userVo);
        return ResultUtils.buildSuccess(users,count);
    }

    /**
     *通过电话号码查询
     * @param phone
     * @return
     */


    @RequestMapping(value = "/queryUserByPhone",method = RequestMethod.GET)
    @ApiOperation(value = "通过电话号码查询用户",notes = "通过电话号码查询用户",httpMethod = "GET")
    @ApiImplicitParam(name = "phone",value = "用户的电话号码",required=true,paramType = "query")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryUserByPhone(@RequestParam(value = "phone")String phone) {
        if (phone==null||phone.equals("")){
            return ResultUtils.buildFail(MessageCode.PHONE_IS_EMPTY);
        }

        List<User> users = userService.queryUserByPhone(phone);

        return ResultUtils.buildSuccess(users);
    }

    /**
     * 通过启用状态查询账户
     * @param status
     * @return
     */

    @RequestMapping(value = "/queryUserByStatus",method = RequestMethod.GET)
    @ApiOperation(value = "通过启用状态查询账户",notes = "通过启用状态查询账户",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "status",value = "启用状态",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryUserByStatus(@RequestParam(value = "status")Integer status,
                                    @RequestParam(value = "page")Integer page,
                                    @RequestParam(value = "limit")Integer limit){

        UserVo userVo=new UserVo();
        userVo.setStartIndex((page-1)*limit);
        userVo.setLimit(limit);
        userVo.setStatus(status);
        List<User> users = userService.queryUserByStatus(userVo);
        Long count = userService.queryUserByStatusCount(status);
        return ResultUtils.buildSuccess(users,count);
    }


    /**
     * 停用账户的状态
     * @param
     * @return
     */
    @RequestMapping(value = "/updateUserByStatus",method = RequestMethod.GET)
    @ApiOperation(value = "停用账户的状态",notes = "停用账户的状态",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "status",value = "启用状态",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result updateUserByStatus(@RequestParam(value = "id")Integer id,
                                     @RequestParam(value = "status")Integer status){
        //TODO
        //判断权限
        userService.updateUserByStatus(id,status);
        return ResultUtils.buildSuccess();
    }


    /**
     * 添加账户
     * @param user
     * @return
     */

    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    @ApiOperation(value = "添加用户",notes = "添加用户",httpMethod = "POST",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result addUser(@RequestBody User user){

        //TODO
        //判断权限
        userService.addUser(user);
        return ResultUtils.buildSuccess();
    }


    @RequestMapping(value = "/queryUserById",method = RequestMethod.GET)
    @ApiOperation(value = "通过账户id查询账户",notes = "通过账户id查询账户",httpMethod = "GET")
    @ApiImplicitParam(name = "id",value = "账户id",required=true,paramType = "query")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryUserById(@RequestParam(value = "id")Integer id){
        User user = userService.queryUserById(id);
        return ResultUtils.buildSuccess(user);
    }




    /**
     * 修改账户信息
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    @ApiOperation(value = "修改账户信息",notes = "修改账户信息",httpMethod = "POST",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result updateUser(@RequestBody User user){
        //TODO
        //判断权限
        if (user.getId()==null||user.getId().equals("")){
            return ResultUtils.buildFail(MessageCode.ID_EMPTY);
        }
        userService.updateUser(user);
        return ResultUtils.buildSuccess();
    }










}
